package com.ssy.lingxi.settle.accounts.serviceimpl;

import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.dto.response.PlatformAccountConfigDetailVO;
import com.ssy.lingxi.settle.accounts.entity.PlatformAccountConfigDO;
import com.ssy.lingxi.settle.accounts.model.constant.AccountConfigUpdateEnum;
import com.ssy.lingxi.settle.accounts.model.vo.request.PlatformAccountConfigUpdateItemVO;
import com.ssy.lingxi.settle.accounts.model.vo.request.PlatformAccountConfigUpdateVO;
import com.ssy.lingxi.settle.accounts.repository.PlatformAccountConfigRepository;
import com.ssy.lingxi.settle.accounts.service.IPlatformAccountConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 平台账户配置接口实现
 * @author 伍将
 * @date 2020/10/20
 * @version 2.0.0
 */
@Service
public class PlatformAccountConfigServiceImpl implements IPlatformAccountConfigService {

    @Resource
    private PlatformAccountConfigRepository repository;

    /**
     * 获取平台账户详情
     * @author 伍将
     * @date 2020/10/20
     * @return com.ssy.lingxi.common.response.Wrapper<com.ssy.lingxi.settle.accounts.mode.vo.response.PlatformAccountConfigDetailVO>
     **/
    @Override
    public Wrapper<PlatformAccountConfigDetailVO> getDetail() {

        PlatformAccountConfigDetailVO result = null;

        PlatformAccountConfigDO configDO = repository.findAll().stream().findFirst().orElse(null);
        if (configDO != null) {
            result = new PlatformAccountConfigDetailVO();
            result.setId(configDO.getId());
            result.setName(configDO.getName());
            result.setBankAccount(configDO.getBankAccount());
            result.setBankDeposit(configDO.getBankDeposit());
        }

        return Wrapper.success(result);
    }

    /**
     * 更新平台账户信息
     * @author 伍将
     * @date 2020/10/20
     * @param updateVO:
     * @return com.ssy.lingxi.common.response.Wrapper
     **/
    @Override
    public Wrapper<Void> update(UserLoginCacheDTO sysUser, PlatformAccountConfigUpdateVO updateVO) {

        PlatformAccountConfigDO configDO = repository.findAll().stream().findFirst().orElse(null);
        if (configDO == null) {
            configDO = new PlatformAccountConfigDO();
        }

        configDO.setName(updateVO.getName());
        configDO.setBankAccount(updateVO.getBankAccount());
        configDO.setBankDeposit(updateVO.getBankDeposit());
        repository.saveAndFlush(configDO);

        return Wrapper.success();
    }

    /**
     * 更新平台账户详情
     * @author 伍将
     * @date 2020/12/17
     * @param updateVO:
     **/
    @Override
    public Wrapper<Void> updateDetail(UserLoginCacheDTO sysUser, PlatformAccountConfigUpdateItemVO updateVO) {

        PlatformAccountConfigDO configDO = repository.findAll().stream().findFirst().orElse(null);
        if (configDO == null) {
            configDO = new PlatformAccountConfigDO();
        }
        AccountConfigUpdateEnum itemEnum = AccountConfigUpdateEnum.getItem(updateVO.getItemType());
        if (itemEnum == null) {
            throw new BusinessException(ResponseCode.SA_ACCOUNT_CONFIG_PARAM_ERROR);
        }
        switch (itemEnum) {
            case NAME:
                configDO.setName(updateVO.getItemValue());
                break;
            case BANK_ACCOUNT:
                configDO.setBankAccount(updateVO.getItemValue());
                break;
            case BANK_DEPOSIT:
                configDO.setBankDeposit(updateVO.getItemValue());
                break;
        }

        repository.saveAndFlush(configDO);

        return Wrapper.success();
    }
}
